package com.time.hellotime.webserver.helper.picasso;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.webkit.URLUtil;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.io.File;

/**
 * Picasso 图片加载类
 * Author: Created by deng on 2016/7/28.
 * E-mail: tx16bing@163.com
 */
public class PicassoImageLoader {

    /**
     * 指定大小加载图片
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param width      宽
     * @param height     高
     * @param mImageView 控件
     */
    public static void loadImageViewSize(
            Context mContext, String path, int width, int height,
            int placeholderResId, int errorResId, String tag, ImageView mImageView) {
        if (TextUtils.isEmpty(path)) {
            return;
        }

        if (URLUtil.isNetworkUrl(path)) {
            Picasso.with(mContext)
                    .load(path)
                    .resize(width, height)
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .into(mImageView);
        } else {
            Picasso.with(mContext)
                    .load(new File(path))
                    .resize(width, height)
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .into(mImageView);
        }
    }

    /**
     * 不指定大小加载图片
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param mImageView 控件
     */
    public static void loadImageViewSize(
            Context mContext, String path,
            int placeholderResId, int errorResId, String tag, ImageView mImageView) {
        if (TextUtils.isEmpty(path)) {
            return;
        }

        if (URLUtil.isNetworkUrl(path)) {
            Picasso.with(mContext)
                    .load(path)
                    .fit()
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .into(mImageView);
        } else {
            Picasso.with(mContext)
                    .load(new File(path))
                    .fit()
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .into(mImageView);
        }
    }

    /**
     * 指定大小加载图片
     * 设置缓存方式
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param width      宽
     * @param height     高
     * @param mImageView 控件
     */
    public static void loadImageViewSizeNoCache(
            Context mContext, String path, int width, int height,
            int placeholderResId, int errorResId, String tag, ImageView mImageView) {
        if (TextUtils.isEmpty(path)) {
            return;
        }

        if (URLUtil.isNetworkUrl(path)) {
            Picasso.with(mContext)
                    .load(path)
                    .resize(width, height)
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(mImageView);
        } else {
            Picasso.with(mContext)
                    .load(new File(path))
                    .resize(width, height)
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .into(mImageView);
        }
    }


    /**
     * 加载有默认图片
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param resId      默认图片资源
     * @param mImageView 控件
     */
    public static void loadImageViewHolder(
            Context mContext, String path,
            int resId, int errorId, ImageView mImageView) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        if (URLUtil.isNetworkUrl(path)) {
            Picasso.with(mContext).load(path).fit().placeholder(resId).error(errorId).into(mImageView);
        } else {
            Picasso.with(mContext).load(new File(path)).fit().placeholder(resId).error(errorId).into(mImageView);
        }
    }


    /**
     * 裁剪图片
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param mImageView 控件
     */
    public static void loadImageViewCrop(Context mContext, String path, ImageView mImageView) {
        if (TextUtils.isEmpty(path)) {
            return;
        }

        if (URLUtil.isNetworkUrl(path)) {
            Picasso.with(mContext).load(path).transform(new CropImageView()).into(mImageView);
        } else {
            Picasso.with(mContext).load(new File(path)).transform(new CropImageView()).into(mImageView);
        }
    }

    public static void loadImageViewBlur(
            Context mContext, String
            path, int width, int height, ImageView mImageView) {
        if (TextUtils.isEmpty(path)) {
            return;
        }

        if (URLUtil.isNetworkUrl(path)) {
            Picasso.with(mContext)
                    .load(path)
                    .resize(width, height)
                    .centerCrop().
                    transform(new BlurTransformation(mContext)).into(mImageView);
        } else {
            Picasso.with(mContext)
                    .load(new File(path))
                    .resize(width, height)
                    .centerCrop().
                    transform(new BlurTransformation(mContext)).into(mImageView);
        }
    }

    /**
     * 指定回调监听
     *
     * @param mContext   上下文
     * @param path       图片路径
     * @param width      宽
     * @param height     高
     * @param mImageView 控件
     */
    public static void loadImageViewCallback(
            Context mContext, String path, int width, int height,
            int placeholderResId, int errorResId, String tag, ImageView mImageView,
            final PicassoCallback callback) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        if (URLUtil.isNetworkUrl(path)) {
            Picasso.with(mContext)
                    .load(path)
                    .resize(width, height)
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .into(mImageView, new Callback() {
                        @Override
                        public void onSuccess() {
                            if (null != callback) {
                                callback.onSucceed();
                            }
                        }

                        @Override
                        public void onError() {
                            if (null != callback) {
                                callback.onFailed();
                            }
                        }
                    });
        } else {
            Picasso.with(mContext)
                    .load(new File(path))
                    .resize(width, height)
                    .centerCrop()
                    .placeholder(placeholderResId)
                    .error(errorResId)
                    .tag(tag)
                    .into(mImageView, new Callback() {
                        @Override
                        public void onSuccess() {
                            if (null != callback) {
                                callback.onSucceed();
                            }
                        }

                        @Override
                        public void onError() {
                            if (null != callback) {
                                callback.onFailed();
                            }
                        }
                    });
        }
    }

    /**
     * 自定义图片裁剪
     */
    public static class CropImageView implements Transformation {

        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;

            Bitmap newBitmap = Bitmap.createBitmap(source, x, y, size, size);

            if (newBitmap != null) {
                //内存回收
                source.recycle();
            }
            return newBitmap;
        }

        @Override
        public String key() {

            return "lgl";
        }
    }

}
